package com.zhc.dejingapi.service.upload.impl;

import com.zhc.dejingapi.pojo.qingflow.QingflowApiInfo;
import com.zhc.dejingapi.service.upload.IQingMoldBindInfoService;
import com.zhc.dejingapi.utils.PubInfoUtils;
import com.zhc.dejingapi.utils.common.DateTimeUtils;
import com.zhc.dejingapi.utils.downqing.QingDownDataSelFieldUtils;
import com.zhc.dejingapi.utils.downqing.QingDownLastDataUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;

@Slf4j
@Service
public class QingMoldBindInfoServiceImpl implements IQingMoldBindInfoService {
    @Autowired
    QingflowApiInfo qqingflowApiInfo;


    @Qualifier("mySqlJdbcTemplate")
    private final JdbcTemplate mySqlJdbcTemplate;

    private final RedisTemplate<String,Object> redisTemplate;


    public QingMoldBindInfoServiceImpl(@Qualifier("mySqlJdbcTemplate")JdbcTemplate mySqlJdbcTemplate1,
                                       @Qualifier("redisTemplateWin")RedisTemplate<String, Object> redisTemplate1) {
        this.redisTemplate = redisTemplate1;
        this.mySqlJdbcTemplate = mySqlJdbcTemplate1;
    }

    @Override
    public Boolean DownMouldEqInfo() {

        String strAppkey="7mrtsr3k1g01";//模具机台
        String strSql="select count(*) from qing_downlstdaterecord where appKey=?";
        int iHaveCount=mySqlJdbcTemplate.queryForObject(strSql,new Object[]{strAppkey},Integer.class);
        Date dLastDate=null;

        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        if(iHaveCount<1){
            strSql="insert  into qing_downlstdaterecord(appKey,lastUpdDate) values(?,?)";
            mySqlJdbcTemplate.update(strSql,new Object[]{strAppkey,null});
            try{
                dLastDate=sdf.parse("2024-08-01 00:00:00");
            }
            catch (Exception e){
                e.printStackTrace();
            }

        }
        else{
            strSql="select lastUpdDate from qing_downlstdaterecord where appKey=?";
            dLastDate=mySqlJdbcTemplate.queryForObject(strSql,new Object[]{strAppkey},Date.class);

        }
        Date dNowDate=new Date();

        QingDownDataSelFieldUtils qingDownLastDataUtils=new QingDownDataSelFieldUtils(strAppkey,mySqlJdbcTemplate,qqingflowApiInfo);

        dLastDate= DateTimeUtils.AddDay(dLastDate,-1);//减少一天
        if(dLastDate==null){
            try{
                dLastDate=sdf.parse("2024-08-01 00:00:00");
            }
            catch (Exception e){
                e.printStackTrace();
            }

        }


        String strminDate=sdf.format(dLastDate);
        String strmaxDate="2090-12-31 23:59:59";
        String strQingFieldTitle="更新时间";
        if(qingDownLastDataUtils.getAppDownData(strQingFieldTitle,strminDate,strmaxDate)){
            strSql="update qing_downlstdaterecord set lastUpdDate=? where appKey=?";
            mySqlJdbcTemplate.update(strSql,new Object[]{dNowDate,strAppkey});
        }

        return null;
    }

    @Override
    public Boolean DownMouldInvInfo() {

        String appKey="7td3l1001o02";//7td3l1001o02	模具&产品绑定
        String strSql="select count(*) from qing_downlstdaterecord where appKey=?";
        int iHaveCount=mySqlJdbcTemplate.queryForObject(strSql,new Object[]{appKey},Integer.class);
        Date dLastDate=null;

        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        if(iHaveCount<1){
            strSql="insert  into qing_downlstdaterecord(appKey,lastUpdDate) values(?,?)";
            mySqlJdbcTemplate.update(strSql,new Object[]{appKey,null});
            try{
                dLastDate=sdf.parse("2024-08-01 00:00:00");
            }
            catch (Exception e){
                e.printStackTrace();
            }

        }
        else{
            strSql="select lastUpdDate from qing_downlstdaterecord where appKey=?";
            dLastDate=mySqlJdbcTemplate.queryForObject(strSql,new Object[]{appKey},Date.class);

        }
        if(dLastDate==null ){
            try{
                dLastDate=sdf.parse("2024-08-01 00:00:00");
            }
            catch (Exception e){
                e.printStackTrace();
            }
        }
        Date dNowDate=new Date();

        PubInfoUtils.LoadMakeList(mySqlJdbcTemplate);

        QingDownLastDataUtils qingDownLastDataUtils=new QingDownLastDataUtils(appKey,mySqlJdbcTemplate,qqingflowApiInfo);
        dLastDate= DateTimeUtils.AddDay(dLastDate,-1);//减少一天
        String strminDate=sdf.format(dLastDate);
        String strmaxDate="2090-12-31 23:59:59";
        String strQingFieldTitle="更新时间";
        Boolean bSuccess=qingDownLastDataUtils.getAppDownData(strQingFieldTitle,strminDate,strmaxDate);
        if(bSuccess){
            strSql="update qing_downlstdaterecord set lastUpdDate=? where appKey=?";
            mySqlJdbcTemplate.update(strSql,new Object[]{dNowDate,appKey});
        }

        return null;
    }

    @Override
    public Boolean DownMouldInfo() {
        String strAppkey="7llu21ac1c01";//模具档案
        String strSql="select count(*) from qing_downlstdaterecord where appKey=?";
        int iHaveCount=mySqlJdbcTemplate.queryForObject(strSql,new Object[]{strAppkey},Integer.class);
        Date dLastDate=null;

        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        if(iHaveCount<1){
            strSql="insert  into qing_downlstdaterecord(appKey,lastUpdDate) values(?,?)";
            mySqlJdbcTemplate.update(strSql,new Object[]{strAppkey,null});
            try{
                dLastDate=sdf.parse("2024-08-01 00:00:00");
            }
            catch (Exception e){
                e.printStackTrace();
            }

        }
        else{
            strSql="select lastUpdDate from qing_downlstdaterecord where appKey=?";
            dLastDate=mySqlJdbcTemplate.queryForObject(strSql,new Object[]{strAppkey},Date.class);

        }
        Date dNowDate=new Date();

        QingDownDataSelFieldUtils qingDownLastDataUtils=new QingDownDataSelFieldUtils(strAppkey,mySqlJdbcTemplate,qqingflowApiInfo);

        if(dLastDate==null){
            try{
                dLastDate=sdf.parse("2024-08-01 00:00:00");
            }
            catch (Exception e){
                e.printStackTrace();
            }

        }
        dLastDate= DateTimeUtils.AddDay(dLastDate,-1);//减少一天
        String strminDate=sdf.format(dLastDate);
        String strmaxDate="2090-12-31 23:59:59";
        String strQingFieldTitle="更新时间";
        if(qingDownLastDataUtils.getAppDownData(strQingFieldTitle,strminDate,strmaxDate)){
            strSql="update qing_downlstdaterecord set lastUpdDate=? where appKey=?";
            mySqlJdbcTemplate.update(strSql,new Object[]{dNowDate,strAppkey});
        }

        return null;
    }
}
